//
//  ViewController.m
//  DelegateStudy
//
//  Created by Jason Len on 2019/9/4.
//  Copyright © 2019年 Jason. All rights reserved.
//

#import "ViewController.h"
#import "GTNormalTableViewCell.h"
#import "GTDetailViewController.h"
#import "GTDeleteCellView.h"
#import "GTListLoader.h"
#import "GTListItem.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,GTNormalTableViewCellDelegate>

@property(nonatomic,strong,readwrite) NSArray<GTListItem *> *dataArray;
@property(nonatomic,strong,readwrite) UITableView *tableView;

@end

@implementation ViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tabBarItem.title=@"新闻";
        self.view.backgroundColor = [UIColor blueColor];
        self.tabBarItem.image=[UIImage imageNamed:@"icon.bundle/page@2x.png"];
        self.tabBarItem.selectedImage=[UIImage imageNamed:@"icon.bundle/page_selected@2x.png"];
        self.view.backgroundColor = [UIColor whiteColor];
        
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _tableView = [[UITableView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_tableView];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    GTListLoader *loader = [[GTListLoader alloc]init];
    
    __weak typeof (self)wself = self; //使用弱引用
    
    [loader loadListDataWithFinishBlock:^(BOOL success, NSArray * _Nonnull dataArray) {
        __strong typeof (wself) strongSelf =wself;
        strongSelf.dataArray = dataArray;
        [strongSelf.tableView reloadData]; //tableView重载数据
        NSLog(@"");
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GTNormalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id"];
    if(!cell){
        cell = [[GTNormalTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"];
    }
    
    cell.delegate = self;
    
    [cell layoutTableViewCellWithListItem:[_dataArray objectAtIndex:indexPath.row]];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    if(indexPath.row % 2 == 0){
        return 100;
//    }else{
//        return 50;
//    }
}
//实现delegate函数
-(void)tableViewCell:(UITableViewCell *)tableViewCell clickDeleteButton:(UIButton *)deleteButton{
//    GTDeleteCellView *deleteView = [[GTDeleteCellView alloc]initWithFrame:self.view.bounds];
//    //将deleteButton的坐标系转换到window坐标系中
//   __weak typeof(self)wSelf = self;
//    CGRect rect= [tableViewCell convertRect:deleteButton.frame toView:nil];
//    [deleteView showDeleteViewFromPoint:rect.origin clickBlock:^{
//
//        __strong typeof(self)strongSelf=wSelf;
//        [strongSelf.dataArray removeLastObject];
//        [strongSelf.tableView deleteRowsAtIndexPaths:@[[strongSelf.tableView indexPathForCell:tableViewCell]] withRowAnimation:UITableViewRowAnimationAutomatic];
//    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@", [NSString stringWithFormat:@"didSelect -%@",@(indexPath.row)]);
    GTListItem *item = [_dataArray objectAtIndex:indexPath.row] ;
    //10. 调用带url参数的初始化详情页的方法
    GTDetailViewController *viewController = [[GTDetailViewController alloc]initWithUrlString:item.articleUrl];
    viewController.navigationItem.title = [NSString stringWithFormat:@"kkkk - %@",@(indexPath.row)];
    
    [self.navigationController pushViewController:viewController animated:YES];
    
    
    
}

@end
